﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DBManager.DanhMucHeThong.BAL;
using DBManager.ChamCong.BAL;
using DBManager.ChamCong.Entities;
namespace MamNonTuoiTho.Form.QuanLyChamCong
{
    public partial class frmChamCong : DevComponents.DotNetBar.Office2007Form
    {
        public frmChamCong()
        {
            InitializeComponent();
            GiaoVien_Load();
            TinhTrang_Load();
            ChamCong_Load();
            dtpNgayCong.Value = DateTime.Now;
        }
        #region LoadData
        private void ChamCong_Load()
        {
            int NgayBatDau = ConvertStringToInt(DateTime.Now.ToString("yyyy/MM/dd"), "/");
            int NgayKetThuc = ConvertStringToInt(DateTime.Now.ToString("yyyy/MM/dd"), "/");
            grvChamCong.AutoGenerateColumns = false;
            grvChamCong.DataSource = ChamCong_BAL.ChamCong_GetItems(NgayBatDau, NgayKetThuc);
        }
        private void GiaoVien_Load()
        {
            cbxGiaoVien.DataSource = DanhMuc_BAL.DanhMuc_GetItems("GiaoVien");
            cbxGiaoVien.DisplayMember = "TenDanhMuc";
            cbxGiaoVien.ValueMember = "DanhMucID";
        }
        private void TinhTrang_Load()
        {
            List<TinhTrangChamCong> lstTinhTrang = new List<TinhTrangChamCong>();
            TinhTrangChamCong item;
            item = new TinhTrangChamCong();
            item.TinhTrangID = 1;
            item.TinhTrang = "Vào đầu giờ";
            lstTinhTrang.Add(item);
            item = new TinhTrangChamCong();
            item.TinhTrangID = 2;
            item.TinhTrang = "Ra cuối giờ";
            lstTinhTrang.Add(item);
            cbxTinhTrang.DataSource = lstTinhTrang;
            cbxTinhTrang.DisplayMember = "TinhTrang";
            cbxTinhTrang.ValueMember = "TinhTrangID";
        }
        #endregion LoadData
        #region Control Handler
            private void Timer_ValueChanged(object sender, EventArgs e)
            {
                string[] aTime = Timer.Value.ToString("HH:mm:ss").Split(":".ToCharArray());
                lblTimer.Text = "Bây giờ là : " + aTime[0] + " giờ " + aTime[1] + " phút " + aTime[2] + " giây";
            }
        #endregion Control Handler

            private void cmdChamCong_Click(object sender, EventArgs e)
            {
                try
                {
                    ChamCong_Ent ChamCong = new ChamCong_Ent();
                    ChamCong.GiaoVienID = int.Parse(cbxGiaoVien.SelectedValue.ToString());
                    ChamCong.GioVao = Timer.Value.ToString("HH:mm");
                    ChamCong.GioVao_So = ConvertStringToInt(Timer.Value.ToString("HH:mm"),":");
                    ChamCong.GioRa = "__:__";
                    ChamCong.GioRa_So = 0;
                    ChamCong.Ngay = dtpNgayCong.Value.ToString();
                    ChamCong.Ngay_So =  ConvertStringToInt(dtpNgayCong.Value.ToString("yyyy/MM/dd"),"/");
                    ChamCong.Tuan = GetWeekNumber(dtpNgayCong.Value);
                    ChamCong.Thang = dtpNgayCong.Value.Month;
                    ChamCong.Nam = dtpNgayCong.Value.Year;
                    ChamCong.GhiChu = txtGhiChu.Text;
                    ChamCong.NgayTao = DateTime.Now.ToString();
                    ChamCong.NguoiTao = 1;
                    ChamCong.NgaySua = DateTime.Now.ToString();
                    ChamCong.NguoiSua = 1;
                    ChamCong_BAL.ChamCong_Add(ChamCong);
                    ChamCong_Load();
                }
                catch (Exception)
                {
                    
                    throw;
                }
            }
        #region Function
            int ConvertStringToInt(string sInput, string CharSpit)
            {
                string[] aInput = sInput.Split(CharSpit.ToCharArray());
                string sRet = "";
                for (int i = 0; i < aInput.Length; i++)
                {
                    sRet += aInput[i];
                }
                return int.Parse(sRet);
            }
            protected int GetWeekNumber(DateTime _date)
            {
                const int JAN = 1;
                const int DEC = 12;
                const int LASTDAYOFDEC = 31;
                const int FIRSTDAYOFJAN = 1;
                const int THURSDAY = 4;
                bool thursdayFlag = false;
                int dayOfYear = _date.DayOfYear;
                DateTime dt = new DateTime(_date.Year, JAN, FIRSTDAYOFJAN);
                int startWeekDayOfYear = (int)(new DateTime(_date.Year, JAN, FIRSTDAYOFJAN).DayOfWeek);
                int endWeekDayOfYear = (int)(new DateTime(_date.Year, DEC, LASTDAYOFDEC).DayOfWeek);
                if (startWeekDayOfYear == 0)
                {
                    startWeekDayOfYear = 7;
                }
                if (endWeekDayOfYear == 0)
                {
                    endWeekDayOfYear = 7;
                }
                int daysInFirstWeek = 8 - (startWeekDayOfYear);
                if (startWeekDayOfYear == THURSDAY || endWeekDayOfYear == THURSDAY)
                {
                    thursdayFlag = true;
                }
                int fullWeeks = (int)(Math.Ceiling((dayOfYear - (daysInFirstWeek)) / 7.0));
                int resultWeekNumber = fullWeeks;
                if (daysInFirstWeek >= THURSDAY)
                {
                    resultWeekNumber = resultWeekNumber + 1;
                }
                if (resultWeekNumber > 52 && !thursdayFlag)
                {
                    resultWeekNumber = 1;
                }
                if (resultWeekNumber == 0)
                {
                    resultWeekNumber = GetWeekNumber(new DateTime(_date.Year - 1, DEC, LASTDAYOFDEC));
                }
                return resultWeekNumber;
            }
        #endregion Function
    }
    #region SubClass
    public class TinhTrangChamCong
    {
        private int _TinhTrangID;
        private string _TinhTrang;
        public int TinhTrangID
        {
            get
            {
                return _TinhTrangID;
            }
            set
            {
                _TinhTrangID = value;
            }
        }
        public string TinhTrang
        {
            get
            {
                return _TinhTrang;
            }
            set
            {
                _TinhTrang = value;
            }
        }
    }
    #endregion SubClass   
}